package com.xywl.escort.view.bandcard

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.*
import com.xywl.escort.common.Constant
import com.xywl.escort.utils.RegexUtils
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_band_card_second_step.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import timber.log.Timber
import javax.inject.Inject

@Route(path = "/xy/band/card/second")
class BandCardSecondStepActivity : BaseActivity(), BandCardSecondStepContract.View {
    @Autowired
    @JvmField
    var dataBean: QueryBandCardTypeResp.DataBean? = null

    @Autowired
    @JvmField
    var idCard = ""

    @Autowired
    @JvmField
    var name = ""

    @Autowired
    @JvmField
    var bandCard = ""

    @Autowired
    @JvmField
    var idType: PayTypeBean? = null

    @Inject
    lateinit var mPresenter: BandCardSecondStepContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_band_card_second_step)
        ARouter.getInstance().inject(this)
        showBackButton()
        setTitleText(R.string.setting_add_band_card)

        EventBus.getDefault().register(this)

        Timber.e("cardType=${dataBean!!.cardType}")
        Timber.e("idCard=$idCard")
        Timber.e("name=$name")
        Timber.e("bandCard=$bandCard")
        Timber.e("mIdType=$idType")
        // CC=贷记卡， DC=借记 卡， SCC=准贷记卡， PC=预付卡
        when (dataBean!!.cardType) {
            "CC" -> {
                layout_security_code.visibility = View.VISIBLE
                layout_validity.visibility = View.VISIBLE
            }
            "DC" -> {
                layout_security_code.visibility = View.GONE
                layout_validity.visibility = View.GONE
            }
            else -> {
                layout_security_code.visibility = View.GONE
                layout_validity.visibility = View.GONE
            }
        }

        mPresenter.takeView(this)
        btn_confirm.setOnClickListener {
            when (dataBean!!.cardType) {
                "CC" -> {
                    submitCc()
                }
                "DC" -> {
                    submitDc()
                }
            }
        }
    }

    private fun submitCc() {
        val account = edit_phone.text.toString().trim()
        val cvv2 = edit_security_code.text.toString().trim()
        val validDate = edit_valid_time.text.toString().trim()
        if (!RegexUtils.checkMobile(account)) {
            ToastUtil.showTip(this, R.string.user_error_hint_phone)
            return
        }
        val req = GetUserBankCardTokenReq()
        req.bankAccountName = name
        req.certificatesType = idType!!.type
        req.certificatesNumber = idCard
        req.bankCardNo = bandCard
        req.phoneNum = account
        mPresenter.getUserBankCardToken(req)
    }

    private fun submitDc() {
        val account = edit_phone.text.toString().trim()
        if (!RegexUtils.checkMobile(account)) {
            ToastUtil.showTip(this, R.string.user_error_hint_phone)
            return
        }
        val req = GetUserBankCardTokenReq()
        req.bankAccountName = name
        req.certificatesType = idType!!.type
        req.certificatesNumber = idCard
        req.bankCardNo = bandCard
        req.phoneNum = account
        mPresenter.getUserBankCardToken(req)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun mainEventBus(event: MessageEvent) {
        Timber.tag("BandCardSecondStepActivity").e("event.message=" + event.message)
        Timber.tag("BandCardSecondStepActivity").e("event.type=" + event.type)
        when (event.type) {
            Constant.EventType.EVENT_ADD_BAND_CARD_TYPE -> {
                val message = event.message
                if (!TextUtils.isEmpty(message)) {
                    if (message == Constant.EventMessage.ADD_BAND_CARD_SUCCESS) {
                        finish()
                    }
                }
            }
            else -> {
            }
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun getUserBankCardTokenSuccess(resp: BaseResp?) {
        Timber.e("resp=$resp")
        val account = edit_phone.text.toString().trim()
        val cvv2 = edit_security_code.text.toString().trim()
        val validDate = edit_valid_time.text.toString().trim()
        ARouter.getInstance()
                .build("/xy/band/card/third")
                .withObject("dataBean", dataBean)
                .withObject("idType", idType)
                .withString("idCard", idCard)
                .withString("name", name)
                .withString("bandCard", bandCard)
                .withString("phone", account)
                .withString("cvv2", cvv2)
                .withString("validDate", validDate)
                .navigation()
    }

    override fun getUserBankCardTokenFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}
